/*
* Copyright © Shanghai Awinic Technology Co., Ltd. 2020-2020. All rights reserved.
* Description: The header file of the UART related driver library.
* Date： 20201126
* Awinic_Version： aw_soc_driver_v1.0.0
*/
#include "compile_option.h"
#ifdef AW_86802

#ifndef __AW8680X_UART_H
#define __AW8680X_UART_H

/* Include ------------------------------------------------------------------*/
#include "aw8680x_def.h"

/**
 * @brief  UART status enumeration variable.
**/
enum uart_status{
	UART_NOEVENT = 0x00U,
	LIB_UART_RX_BUSY = 0x01U,
	LIB_UART_RX_END = 0x02U,
};
typedef enum uart_status UART_STATUS_TYPE_E;

/**
 * @brief  UART gpio pin chose enumeration variable.
**/
enum uart_gpio_pin{
	RX_GPIO_10 = 20U,
	TX_GPIO_11 = 22U,
	TX_GPIO_12 = 24U,
	RX_GPIO_13 = 26U,
};
typedef enum uart_gpio_pin GPIO_PIN_TYPE_E;

/**
 * @brief  UART port data length enumeration variable.
**/
enum uart_word_len{
	UART_WORD_LEN_5BIT = 0x00U,
	UART_WORD_LEN_6BIT = 0x01U,
	UART_WORD_LEN_7BIT = 0x02U,
	UART_WORD_LEN_8BIT = 0x03U,
};
typedef enum uart_word_len UART_WORD_LEN_TYPE_E;

/**
 * @brief  Uart Initialization Configuration Structure definition.
**/
struct uart_init_struct {
	AW_U8 stop_bit;		// Specifies the number of stop bits transmitted.
	AW_U8 pen_bit;		// Calibration enable bit.
	AW_U8 eps_bit;		// Parity selection bit.
	AW_U8 stick_p_bit;	// Parity selection control bit.
	AW_U8 bc_bit;		// Break control bit,Send a break bit to the receiver in case of system 1.
	GPIO_PIN_TYPE_E gpio_rx;
	GPIO_PIN_TYPE_E gpio_tx;
	UART_WORD_LEN_TYPE_E uart_word_lenth_e;	// UART port data length enumeration variable.
	AW_U32 sys_clk;							// System clock.
	AW_U32 baud_rate;						// This member configures the UART communication baud rate.
};
typedef struct uart_init_struct UART_INIT_TYPE_S;

/**
 * @brief  Uart Configuration Structure definition.
**/
struct uart_handle_struct {
	IRQ_PRI_E uart_irq_pri_e;				// UART irq priority.
	UART_INIT_TYPE_S init_s;				// UART communication parameters.
	__IO UART_STATUS_TYPE_E uart_stat_e;	// UART current state.
};
typedef struct uart_handle_struct UART_HANDLE_TYPE_S;

/* UART word length */
#define UART_CHECK_WORD_LEN(uart_word_lenth) (((uart_word_lenth) == UART_WORD_LEN_5BIT) || \
												((uart_word_lenth) == UART_WORD_LEN_6BIT) || \
												((uart_word_lenth) == UART_WORD_LEN_7BIT) || \
												((uart_word_lenth) == UART_WORD_LEN_8BIT))

#define USR_BUSY_EN			((AW_U32)0x00000001)
#define USR_BUSY_DIS		(~UART_BUSY_EN)
#define USR_TFNF_EN			((AW_U32)0x00000002)
#define USR_TFNF_DIS		(~USR_TFNF_EN)

#define LSR_DR_EN			((AW_U32)0x00000001)
#define LSR_DR_DIS			(~LSR_TEMT_EN)
#define LSR_TEMT_EN			((AW_U32)0x00000040)
#define LSR_TEMT_DIS		(~LSR_TEMT_EN)
#define LSR_RFE_EN			((AW_U32)0x00000080)
#define LSR_RFE_DIS			(~LSR_RFE_EN)

#define IIR_FIFOSE_EN		((AW_U32)0x000000C0)
#define IIR_FIFOSE_DIS		(~IIR_FIFOSE_EN)
#define IID_FULL			((AW_U32)0x0000000F)
#define IID_VALID			((AW_U32)0x00000004)
#define IID_TIMEOUT			((AW_U32)0x0000000C)

#define LCR_DLAB_EN			((AW_U32)0x00000080)
#define LCR_DLAB_DIS		(~LCR_DLAB_EN)
#define LCR_STOP_BIT		(2)
#define LCR_PEN_BIT			(3)
#define LCR_EPS_BIT			(4)
#define LCR_STICK_P_BIT		(5)
#define LCR_BC_BIT			(6)
#define LCR_DLAB_BIT		(7)

#define FCR_FIFOE_EN		((AW_U32)0x00000001)
#define FCR_FIFOE_DIS		(~FCR_FIFOE_EN)
#define FCR_RFIFOR_EN		((AW_U32)0x00000002)
#define FCR_RFIFOR_DIS		(~FCR_RFIFOR_EN)
#define FCR_XFIFOR_EN		((AW_U32)0x00000004)
#define FCR_XFIFOR_DIS		(~FCR_XFIFOR_EN)
#define FCR_TFT_EMPT		((AW_U32)0xFFFFFFCF)
#define FCR_TFT_TWO			((AW_U32)0x00000010)
#define FCR_TFT_QUARTER		((AW_U32)0x00000020)
#define FCR_TFT_HALF		((AW_U32)0x00000030)
#define FCR_RCVR_ONE		((AW_U32)0xFFFFFF3F)
#define FCR_RCVR_QUARTER	((AW_U32)0x00000040)
#define FCR_RCVR_HALF		((AW_U32)0x00000080)
#define FCR_RCVR_TWO		((AW_U32)0x000000C0)

#define IER_ERBFI_EN		((AW_U32)0x00000001)
#define IER_ERBFI_DIS		(~IER_ERBFI_EN)

/**
 * @brief  UART macro definition function
**/
#define SERIAL_PUT_STRING(x)	uart_send_string((AW_U8 *)(x))
#define UART_TIMEOUT			(11500)		// Timeout judgment 10ms.

#ifdef UART_TEST
/**
 * @brief  UART global variables
**/
	#define UART_BUFF_LEN		(14U) // test Hello awinic
	#define BAUD_RATE			((AW_U32)9600)

	extern UART_STATUS_TYPE_E g_uart_status;
	extern AW_U8 g_uart_rx_buff[UART_BUFF_LEN]; // uart receive buff
	extern AW_U8 g_uart_data_cent;
#endif // UART_TEST

RET_STATUS_E uart_init(UART_HANDLE_TYPE_S *p_uart_lib);
RET_STATUS_E uart_send_1byte(AW_U8 send_data);
RET_STATUS_E uart_send_string(AW_U8 *p_send_data);
RET_STATUS_E uart_irq(void);

#endif
#endif
